home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / DATA_MG.ZIP / STACK.C < prev   
Encoding:
C/C++ Source or Header  |  1990-03-30  |  2.0 KB  |  102 lines

  1. /*
  2. **  STACK.C - A Simple Stack routine
  3. **
  4. **
  5. **  This file contains the Push() and Pop() functions for implementing a
  6. **  Stack in 'C'.  The stack is actually an array of integers, fixed in
  7. **  size which does not really follow the definition of a stack which is:
  8. **  A dynamically shrinking and growing LIFO data structure.
  9. **
  10. **  Several uses of the Stack in actual practice are implemented below such
  11. **  as local variable storage, parameter passing and recuresion.  This
  12. **  file is for academic use only and is not intended as an efficient
  13. **  stack system for actual useage.
  14. **
  15. **
  16. **  To Compile:   TCC or QCL  STACK.C
  17. **
  18. **  Mario Giannini
  19. **
  20. */
  21. #define MAXSTACKSIZE 15  /* Keep the stack small so error arises and are seen */
  22.  
  23. struct _STACK_ {
  24.     int Pointer, Elements[MAXSTACKSIZE];
  25.     } RealStack;
  26.  
  27. main()
  28. {
  29.     int n;
  30.     RealStack.Pointer=0;
  31.     do {
  32.         printf("\n1 Add\n2 Get ");
  33.         n=getch();
  34.         if(n=='1')
  35.         {
  36.             printf("\nEnter an integer: ");
  37.             scanf("%d", &n);
  38.             Push(n);
  39.         }
  40.         else
  41.         if(n=='2')
  42.         {
  43.             n=Pop();
  44.             printf("\n\nValue is %d\n", n);
  45.         }
  46.     } while(n!=27);
  47.  
  48.     printf("\n\n");
  49.     Push(3); /* Parameters to the AddEm function */
  50.     Push(4);
  51.     Push(5);
  52.     Push(3);
  53.     AddEm();
  54.  
  55.     printf("%d\n", Recursive());
  56.  
  57. }
  58.  
  59.  
  60. Push(int n)
  61. {
  62.     if(RealStack.Pointer==MAXSTACKSIZE)
  63.     {
  64.         printf("Stack overflow\n");
  65.         return;
  66.     }
  67.     RealStack.Elements[RealStack.Pointer++]=n;
  68. }
  69.  
  70. Pop()
  71. {
  72.     int n;
  73.     if(!RealStack.Pointer)
  74.     {
  75.         printf("Stack underflow\n");
  76.         return(0);
  77.     }
  78.     n=RealStack.Elements[--RealStack.Pointer];
  79.     return(n);
  80. }
  81.  
  82. AddEm()  /* An example of parameter passing via the Stack */
  83. {
  84.     int i=0, j;
  85.     
  86.     j=Pop();
  87.     while(j--)
  88.         i+=Pop();
  89.     printf("The total is %d\n", i);
  90. }
  91.  
  92. Recursive()  /* An example of recursion and local storage via the Stack */
  93. {
  94.     Push(0);
  95.  
  96.     printf("Enter an integer (0 stops): ");
  97.     scanf("%d", &RealStack.Elements[RealStack.Pointer-1]);
  98.     if(RealStack.Elements[RealStack.Pointer-1]!=0)
  99.         RealStack.Elements[RealStack.Pointer-1]+=Recursive();
  100.     return(Pop());
  101. }
  102.